from __future__ import annotations
from typing import ( Any,  Callable, Dict, Iterable,  List,Optional,Tuple,Type,Union,  TypedDict)
from ApiBase import apiBase

#向量对象
class VectInfo(TypedDict):
    id: int=None
    # 向量的名称
    name: str
    # 向量算法名称"VCT_ADJUST",
    vctname: str="VCT_ADJUST",
    # 取多少个向量
    topn: int=3
    # 向量的开始位置
    start: int=0
    # 过滤的阀门
    threshold: float=0.3
    #prompt_id的内容
    prompt_id: int=None
    # 函数名称
    fun_name: str
    # 强制包含的条件
    where: str

# 工具对象
class ToolInfo(TypedDict):
    id: int
    # 类型,python,hpl,sql,api
    type :str
    # 名称
    name: str
    # 描述
    desc: str
    # 代码
    code: str
    # 函数
    fun: Any

class ApiModels():
    def __init__(self):
        apiBase.getLLMConnect()
    def get_vects(self,names:list,ids:list=None):
        if  ids:
            vcts=[]
            for id in ids:
                vt=self.get_vect(id=id)
                vcts.append(vt)        
            return vcts
        if names:
            vcts=[]
            for collect in names:
                vt=self.get_vect(name=collect)
                vcts.append(vt)        
            return vcts        
        return None 
    
    def get_vect(self,name=None,id=None,prompt_id=None,vctname='VCT_ADJUST',topn=6,start=0,threshold=0.3,where=None) -> VectInfo:
        vt=None
        if id:
            sql=f"select * from  ent_vector where id={id}"
            vts=apiBase.query_json(sql)
            if len(vts) > 0:
              vt=vts[0]
        if vt is None:
            vt=VectInfo(id=id,name=name,prompt_id=prompt_id,vctname=vctname,topn=topn,start=start,threshold=threshold,fun_name="qa",where=where)
        return vt
    
    def get_tool(self,name=None) -> ToolInfo:
        vt=None
        if name:
            sql=f"select * from ent_tool where name='{name}'"
            vts=apiBase.query_json(sql)
            if len(vts) > 0:
              vt=vts[0]
        if vt is None:
           return None
        if vt['code']:
            vt['fun']=apiBase.create_tool(name,vt['remark'],vt['code'],'code')
        return vt    
        
apiModels=ApiModels()
#apiModels.get_tool(id=1)
#apiModels.get_vect('proc/extract/tablename',1)
#apiModels.get_vect_names(['proc/extract/tablename'])
#apiModels.get_vect_ids([1])